#region imported Namespaces
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

using BO = PHSRAG.Insight21.BusinessObjects;
using DataAccess = PHSRAG.Utility.DataAccess;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using SalaryConstants = PHSRAG.Insight21.Personnel.Constants.SalaryAdjustmentsConstants;
using ArgumentIndex = PHSRAG.Insight21.Personnel.PersonnelDetailsArgumentIndex;
#endregion

namespace PHSRAG.Insight21.Personnel
{
	#region Salaries Class
	/// <summary>
	/// The Salaries class supports the loading of salary adjustment information for 
	/// a given job map ID and acts as a container for salary object.
	/// It also provides indexing support to access the contained Salary objects
	/// </summary>
	public class Salaries : BO.BusinessObject, IEnumerable
	{
		#region Instance Variables
		private ArrayList salarys;
		#endregion

		#region Public properties
		/// <summary>
		/// Get the count of Salary objects contained
		/// </summary>
		public int Count
		{
			get{return salarys.Count;}
		}
		#endregion

		#region Indexers
		/// <summary>
		/// Retrieve a Salary from the Salaries instance, given 
		/// its ordinal position within the collection. If the ordinal position is 
		/// out of region, a null cast exception is thrown 
		/// </summary>
		public Salary this[int index]
		{
			get{return (Salary)salarys[index];}
		}
		#endregion

		#region Constructor
		/// <summary>
		/// Intializes the base class with the cache policy and set the DataContainer of the present instance
		/// </summary>
		/// <param name="dataContainer">Dataset passed from the calling class</param>
		/// <param name="cachePolicy">The prevalent cache policy</param>
		public Salaries(DataSet dataContainer,ICachePolicy cachePolicy) : base(cachePolicy)
		{
			DataContainer = dataContainer;
		}
		#endregion

		#region IEnumerable Members
		/// <summary>
		/// Get the enumerator associated with the Salaries instance
		/// </summary>
		/// <returns>Object of type IEnumerator</returns>
		public IEnumerator GetEnumerator()
		{
			return salarys.GetEnumerator();
		}

		#endregion

		#region Public Instance Methods
		/// <summary>
		/// Creates a new salary object after adding a salary row to the appropriate
		/// table in the datacontainer and adding a salary object to the contained arraylist
		/// </summary>
		/// <returns>An empty salary object</returns>
		public Salary CreateNewSalary()
		{
			DataTable salaryTable = DataContainer.Tables[SalaryConstants.SalaryAdjustmentsTableName];
			
			DataRow salaryRow = salaryTable.NewRow();
			salaryTable.Rows.Add(salaryRow);
			
			Salary salary = new Salary(salaryRow);
			salarys.Add(salary);			
			return salary;
		}
		/// <summary>
		/// Remove a salary adjustment object from the arraylist and
		/// also removes the associated row from the datacontainer
		/// </summary>
		/// <param name="salaryIndex">Index at which the salary object has to be removed</param>
		public void RemoveSalary(int salaryIndex)
		{
			salarys.RemoveAt(salaryIndex);
			DataContainer.Tables[SalaryConstants.SalaryAdjustmentsTableName].Rows.RemoveAt(salaryIndex);
		}
		#endregion

		#region BusinessObject methods
		/// <summary>
		/// This method is not used and only exists to satisfy interface requirements.
		/// </summary>
		public override void Cache()
		{
			throw new Exception("The salaries object cannot be cached independently");
		}
		/// <summary>
		/// This method is not used and only exists to satisfy interface requirements.
		/// </summary>
		public override void UnCache()
		{
			throw new Exception("The salaries object cannot be uncached independently");
		}
		/// <summary>
		/// Load the salary adjustment data accessible to the currently logged in user and
		/// load the contained arraylist with as many Salary objects as the number of rows in the salary table
		/// </summary>
		/// <param name="connectionString">The database connection string</param>
		/// <param name="args">Is expected to contain 2 mandatory arguments - DomainUserID,EmployeeJobMapID</param>
		public override void Load(string connectionString, params object[] args)
		{
			if (args.Length != 2)
				throw new Exception("Salaries load requires the domainuserid and a jobmapid");
			
			DataAccess.LoadDataSet
				(DataContainer
				,SalaryConstants.SalaryAdjustmentsTableName
				,connectionString
				,SalaryConstants.GetSalaryAdjustmentsSPROCName
				,Utility.DataAccess.BuildSqlParameterArray
				("@domainUserID",SqlDbType.Int,args[(int)ArgumentIndex.DomainUserID]
				,"@employeeJobMapID", SqlDbType.Int, args[(int)ArgumentIndex.EmployeeJobMapID]));

			salarys = new ArrayList();
			foreach(DataRow salaryRow in 
				DataContainer.Tables[SalaryConstants.SalaryAdjustmentsTableName].Rows)
			{
				salarys.Add(new Salary(salaryRow));
			}
		}
		#endregion
	}

	#endregion
}